package com.apes.fn.business.insurance.modle;

import com.alibaba.fastjson.JSONObject;
import com.apes.crm.business.model.InsuranceCompany;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;

/**
 * @program: apes-server
 * @description 车险有效保单修改
 * @author: czs
 * @create: 2022-02-10 11:44
 **/
@Setter
@Getter
@Entity
@SelectBeforeUpdate
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsurancePolicyFixItem.CONST_TABLE_NAME)
@DynamicInsert
@DynamicUpdate
public class InsurancePolicyFixItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "insurance_Policy_fix_item";

    @Id
    @Column(name = "id", nullable = false)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "修改明细编码", padding = 10,prefix = "1")
    @Comment("修改明细编码")
    private String id;

    public void setPolicyNumber(String policyNumber) {
        if (policyNumber==null)return;
        this.policyNumber = policyNumber;
    }

    public String getPolicyNumber() {
        return policyNumber;
    }

    @Column(name = "policy_Number",length = 64)
    @Comment("保单号")
    private String policyNumber;

    public void setPolicyItemNumber(String policyItemNumber) {
        if (policyItemNumber==null)return;
        this.policyItemNumber = policyItemNumber;
    }

    @Column(name = "policy_item_number",length = 64)
    @Comment("保单明细号")
    private String policyItemNumber;
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
//    @Comment("供应商")
//    private PartyRole supplier;

    @Column(name = "insurance_company")
    @Comment("保险公司")
    private String insuranceCompany;

    public void setInsuranceType(String insuranceType) {
        switch (insuranceType){
            case "0":
                insuranceType="商业险";
                break;
            case "1":
                insuranceType="交强险";
                break;
            case "2":
                insuranceType="驾意险";
                break;
                default:
                    insuranceType=insuranceType;
        }
        this.insuranceType = insuranceType;
    }

    @Comment("险种")
    @Column(name = "insurance_type")
    private String insuranceType ;

    @Comment("保险金额")
    @Column(name = "insurance_amount",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double insuranceAmount;

    @Comment("净保费")
    @Column(name = "net_Premium",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double netPremium;

//    @States(name = "佣金类型", values = {
//            @State(id = "A", name = "正常佣金"),
//            @State(id = "B", name = "追加佣金"),
//            @State(id = "C", name = "退保佣金"),
//            @State(id = "D", name = "退保追加佣金")
//    })
//    @Comment("佣金类型")
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "commission_type", nullable = false, referencedColumnName = "id")
//    private BusinessAttributeValue commissionType;


    @Column(name = "commission_type")
    @Comment("佣金类型")
    private String commissionType;

    @Comment("原佣金比率")
    @Column(name = "commission_Ratio",nullable = false, columnDefinition = ApesConst.FIELD_ROUND_16_8)
    private double commissionRatio = 0;

    @Comment("保险公司修改后佣金比率")
    @Column(name = "fix_commission_Ratio",nullable = false, columnDefinition = ApesConst.FIELD_ROUND_16_8)
    private double fixCommissionRatio = 0;

    @Comment("佣金金额")
    @Column(name = "commission",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double commission = 0 ;

    @Comment("修改后佣金金额")
    @Column(name = "fix_commission",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double fixCommission = 0;

    @Comment("原赋能佣金比率")
    @Column(name = "fn_commission_Ratio",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double fnCommissionRatio = 0 ;

    @Comment("原赋能佣金金额")
    @Column(name = "fn_commission_amount",nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double fnCommissionAmount = 0 ;

    @Comment("赋能修改后比率")
    @Column(name = "fix_fn_commission_Ratio",nullable = false,  columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double fixFnCommissionRatio = 0;

    @Comment("修改后赋能佣金金额")
    @Column(name = "fix_fn_commission_amount",nullable = false,  columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double fixFnCommissionAmount = 0;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "insurance_policy_fix",referencedColumnName = "id")
    private InsurancePolicyFix insurancePolicyFix;

    public JSONObject getPolicyJson() {
        return new JSONObject(MapUtil.mapper("id",policyNumber));
    }

    public void setPolicyJson(JSONObject policyJson) {
       setPolicyNumber(policyJson.getString("id"));
       setPolicyItemNumber(policyJson.getString("itemId"));
    }

    @Transient
    private JSONObject policyJson;


    @Transient
    private boolean selected;

    @Transient
    private int sort;

    @Transient
    private Boolean delete;
}
